package com.iemes.controller.transport;

import java.util.List;
import java.util.Map;

import javax.inject.Inject;
import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import com.iemes.annotation.SystemLog;
import com.iemes.controller.index.BaseController;
import com.iemes.entity.MotorcadeInfoFormMap;
import com.iemes.entity.TransportApplyFormMap;
import com.iemes.entity.UserFormMap;
import com.iemes.mapper.motorcade.MotorcadeMapper;
import com.iemes.mapper.transport.TransportApplyMapper;
import com.iemes.plugin.PageView;
import com.iemes.util.Common;
import com.iemes.util.DateUtils;

@RequestMapping("/transportapply/")
@Controller
public class TransportApplyController extends BaseController{
	
	@Inject
	private TransportApplyMapper transportApplyMapper;
	@Inject
	private MotorcadeMapper motorcadeMapper;
	
	//查看运输申请表
	@RequestMapping("/examinetransportapply")
	public String examineTransport(Model model){
		model.addAttribute("res", findByRes());
		return Common.BACKGROUND_PATH+"/transport/apply/transport_list";
	}
	
	//运输申请表分页
	@ResponseBody
	@RequestMapping("/transportapply_findbyPage")
	public PageView findByPage(String pageNow, String pageSize) throws Exception 
	{
		TransportApplyFormMap transportApplyFormMap=getFormMap(TransportApplyFormMap.class);
		List<Map<String, Object>> transportApplyFormMaps = transportApplyMapper.getAllInfo(transportApplyFormMap);
//		TransportApplyFormMap transportApplyFormMap=getFormMap(TransportApplyFormMap.class);
//		List<Map<String, Object>> transportApplyFormMaps=transportApplyMapper.getAllInfo(transportApplyFormMap);
//		for (Map<String, Object> map : transportApplyFormMaps) {
//			List<Map<String, Object>> transpor=transportApplyMapper.getAll(Integer.valueOf(map.get("licenseNo").toString()));
//			map.put("transpor", transpor);
//		}
		LuogicPaging(pageNow, pageSize, transportApplyFormMaps);
		pageView.setRecords(transportApplyFormMaps);
		return pageView;
	}
	//修改车辆申请表
	@RequestMapping("/transport_edit")
	public String tranup(Model model){
		String id = getPara("id");
		List<Map<String, Object>> transportApplyFormMaps=transportApplyMapper.getAllInfo(null);

		//获取对象
		if(Common.isNotEmpty(id))
		{
			TransportApplyFormMap transportApplyFormMap= baseMapper.findbyFrist("id", id, TransportApplyFormMap.class);
			for (Map<String, Object> map : transportApplyFormMaps) {
				if(map.get("id")==transportApplyFormMap.get("id")){
					transportApplyFormMap.put("carBossName", map.get("carBossName"));
					transportApplyFormMap.put("carBossNumber", map.get("carBossNumber"));
				}
			}
			model.addAttribute("tranedit", transportApplyFormMap);
		}
		return Common.BACKGROUND_PATH + "/transport/apply/transport_edit";
	}
	@ResponseBody
	@RequestMapping(value="/transport_editEntity")
	@SystemLog(module="车队运输申请",methods="申请-编辑申请")//凡需要处理业务逻辑的.都需要记录操作日志
	@Transactional
	public String editEntity() {
		TransportApplyFormMap transportApplyFormMap=getFormMap(TransportApplyFormMap.class);
		String oldlicenseNo=transportApplyFormMap.getStr("oldlicenseNo");
		MotorcadeInfoFormMap motorcadeInfoFormMap=motorcadeMapper.findbyFrist("licenseNo", oldlicenseNo, MotorcadeInfoFormMap.class);
		if(oldlicenseNo.equals(transportApplyFormMap.getStr("licenseNo"))){
			motorcadeInfoFormMap.put("carBossName", transportApplyFormMap.get("carBossName"));
			motorcadeInfoFormMap.put("carBossNumber", transportApplyFormMap.get("carBossNumber"));
		}else{
			motorcadeInfoFormMap.put("licenseNo", transportApplyFormMap.getStr("licenseNo"));
			motorcadeInfoFormMap.put("carBossName", transportApplyFormMap.get("carBossName"));
			motorcadeInfoFormMap.put("carBossNumber", transportApplyFormMap.get("carBossNumber"));
		}
		try {
			transportApplyMapper.editEntity(transportApplyFormMap);
			motorcadeMapper.editEntity(motorcadeInfoFormMap);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return "fasle";
		}
		return "success";
	}
	
	
	//添加车辆申请表
	@RequestMapping("/transport_add")
	public String addUI(Model model){
		List<MotorcadeInfoFormMap> motorcadeInfoFormMap=motorcadeMapper.findByWhere(new MotorcadeInfoFormMap());
		model.addAttribute("motorcadeInfoFormMap", motorcadeInfoFormMap);
		return Common.BACKGROUND_PATH + "/transport/apply/transport_add";
	}
	
	@RequestMapping("/transport_id")
	@ResponseBody
	public MotorcadeInfoFormMap trid(){
		String id=getPara("id");
		MotorcadeInfoFormMap motorcadeInfoFormMap=motorcadeMapper.findbyFrist("id", id, MotorcadeInfoFormMap.class);
		return motorcadeInfoFormMap;
	}
	
	
	@ResponseBody
	@RequestMapping("/transport_addEntity")
	@SystemLog(module="车队运输申请",methods="申请-新增申请")
	public String addEntity(){
		TransportApplyFormMap transportApplyFormMap=getFormMap(TransportApplyFormMap.class);
		// 获取request
		HttpServletRequest request = ((ServletRequestAttributes)RequestContextHolder.getRequestAttributes()).getRequest();  
		// 通过工具类获取当前登录的bean
		UserFormMap userFormMap = (UserFormMap)Common.findUserSession(request);
		transportApplyFormMap.put("byuser_id", userFormMap.getInt("id"));
		transportApplyFormMap.set("signDate", DateUtils.getStringDateTime());
		try {
			transportApplyMapper.addEntity(transportApplyFormMap);
		} catch (Exception e) {
			return "false";
		}
		return "success";
	}
	
	//删除车队运输申请
			@ResponseBody
			@RequestMapping("transport_deleteEntity")
			@Transactional(readOnly=false)//需要事务操作必须加入此注解
			@SystemLog(module="删除车队运输申请",methods="pod按钮-删除pod按钮")//凡需要处理业务逻辑的.都需要记录操作日志
			public String deleteEntity() throws Exception {
				TransportApplyFormMap transportApplyFormMap=getFormMap(TransportApplyFormMap.class);
				String[] ids = getParaValues("ids");
				String arr=ids[0];
				String[] str=arr.split(",");
				for(int i=0;i<str.length;i++){
					transportApplyFormMap.set("status", 2);
					transportApplyFormMap.put("id", str[i]);
					transportApplyMapper.editEntity(transportApplyFormMap);
				}
				
				return "success";
			}
	
}
